package cn.bdqn.service.sys.impl;

import cn.bdqn.dao.sys.SysSignDao;
import cn.bdqn.entity.SignImage;
import cn.bdqn.service.sys.SysSignService;
import cn.bdqn.utils.UploadFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @ProjectName: ycbdqn
 * @Package: cn.bdqn.service.sys.impl
 * @Author: huat
 * @Date: 2020/3/12 10:41
 * @Version: 1.0
 */
@Service
public class SysSignServiceImpl implements SysSignService {
    @Autowired
    SysSignDao sysSignDao;
    @Value("${route_name}")
    private String routeName;//获取图片前面路径
    /**
     * 查询活动
     * @param page 起始页
     * @param limit 页面大小
     * @param signName  活动名称
     * @return
     */
    @Override
    public Map<String, Object> getSign(int page, int limit, String signName) {
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("code",0);
        map.put("data",sysSignDao.getSign((page-1)*limit, limit, signName));
        map.put("count",sysSignDao.countSign(signName));
        return map;
    }
    /**
     * 通过活动id删除活动
     * @param signId
     * @return
     */
    @Override
    @Transactional
    public boolean deleteSign(int signId) {
        return sysSignDao.deleteSign(signId)>0?true:false;
    }
    /**
     * 根据id查询活动
     * @param signId 活动id
     * @return
     */
    @Override
    public SignImage getSignById(int signId) {
        return sysSignDao.getSignById(signId);
    }
    /**
     * 修改活动图
     * @param request
     * @param signImage
     * @return
     */
    @Override
    @Transactional
    public String updateSign(HttpServletRequest request, SignImage signImage) {
        MultipartHttpServletRequest mr = (MultipartHttpServletRequest) request;

       if(null!=mr.getMultiFileMap().get("imageUrl")){
           String fileName=mr.getFileMap().get("imageUrl").getOriginalFilename();
           String fileNameIsJPG=fileName.substring(fileName.lastIndexOf(".")+1);
           if(!"jpg".equalsIgnoreCase(fileNameIsJPG)){
               return "2";
           }
           String result=UploadFile.getFileURL(mr,"imageUrl");

           signImage.setSignImageUrl(routeName+"/image/"+result);
       }

        return sysSignDao.updateSign(signImage)>0?"1":"0";
    }

    /**
     * 添加活动
     * @param request
     * @param signImage
     * @return
     */
    @Transactional
    @Override
    public String addSign(HttpServletRequest request, SignImage signImage) {
        MultipartHttpServletRequest mr = (MultipartHttpServletRequest) request;

        if(null!=mr.getMultiFileMap().get("imageUrl")){
            String fileName=mr.getFileMap().get("imageUrl").getOriginalFilename();
            String fileNameIsJPG=fileName.substring(fileName.lastIndexOf(".")+1);
            if(!"jpg".equalsIgnoreCase(fileNameIsJPG)){
                return "2";
            }
            String result=UploadFile.getFileURL(mr,"imageUrl");

            signImage.setSignImageUrl(routeName+"/image/"+result);
        }else{
            return "2";
        }

        return sysSignDao.addSign(signImage)>0?"1":"0";
    }
}
